package com.work.xxzl.module.main;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

import com.work.xxzl.R;
import com.work.xxzl.cache.Ckey;
import com.work.xxzl.cache.SPutils;
import com.work.xxzl.utils.TelManagerUtils;
import com.work.xxzl.utils.UIUtils;


/**
 * author：WangZhan
 * data: 2017/1/14 09:00
 * version: 1.0.0
 * explain:闪屏界面
 */

public class SplashActivity extends FragmentActivity {
    private Intent intent;
    //版本号
    private String versionCode;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String isfirst = SPutils.get(Ckey.ISFIRST);
            if ("true".equals(isfirst)) {
                //第一次启动进入引导页
                intent = new Intent(SplashActivity.this, GuideActivity.class);
            } else {
                //进入主界面
                intent = new Intent(SplashActivity.this, MainActivity.class);
            }
            UIUtils.startActivity(intent);
            handler.removeCallbacksAndMessages(null);
            finish();
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// 去掉信息栏
        setContentView(R.layout.activity_splash);
        initData();

        //把版本号存进缓存为了下次启动的时候判断用户是否已经更新到新版本
        versionCode = TelManagerUtils.getVersionCode(this)+"";
        if (versionCode.equals(SPutils.get(Ckey.VERSIONCODE))) {
            SPutils.put(Ckey.ISFIRST, "false");
        } else {
            SPutils.put(Ckey.VERSIONCODE, versionCode);
            SPutils.put(Ckey.ISFIRST, "true");
        }
    }


    private void initData() {
        handler.sendEmptyMessageDelayed(0, 3000);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 在欢迎界面屏蔽BACK键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return false;
        }
        return false;
    }
}
